package com.prestamomo.vil.ppa.pmvilpage.pmfg;

import android.app.AlertDialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.prestamomo.vil.ppa.BuildConfig;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmCheckLoan;
import com.prestamomo.vil.ppa.pmvilapi.PmInitLoan;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerConfig;
import com.prestamomo.vil.ppa.pmvilapi.PmvilUserRz;
import com.prestamomo.vil.ppa.pmvilmedium.CheckLocationUtil;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilPermissMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilview.PmvilLimitView;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilSpMd;
import com.prestamomo.vil.ppa.pmvilnet.PmvilAjustinfse;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.PmvilBlockVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilCensorVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilListingVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilPlaceVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilSellerVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilSourceVy;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilFgRoot;

public class PmvilOwerFg extends PmvilFgRoot {

    private TextView fgowerPmvilEdsq;
    private RelativeLayout fgowerPmvilRled;
    private RelativeLayout fgowerPmvilRljswc;
    private TextView fgowerPmvilTvjsed;
    private TextView fgowerPmvilTvsq;

    @Override
    public int setPmvilLayoutId() {
        return R.layout.fg_ower_pmvil_ly;
    }

    @Override
    public void findPmvilViewById(View view) {
        fgowerPmvilEdsq = view.findViewById(R.id.fgower_pmvil_edsq);
        fgowerPmvilRled = view.findViewById(R.id.fgower_pmvil_rl_ed);
        fgowerPmvilRljswc = view.findViewById(R.id.fgower_pmvil_rl_jswc);
        fgowerPmvilTvjsed = view.findViewById(R.id.fgower_pmvil_tv_jsed);
        fgowerPmvilTvsq = view.findViewById(R.id.fgower_pmvil_tv_sq);

        Boolean isPmvilEd = (Boolean) PmvilSpMd.getPmvaParam(pmvilAtyRoot, "is_pmvil_ed", false);
        if(isPmvilEd){
            fgowerPmvilRled.setVisibility(View.GONE);
            fgowerPmvilRljswc.setVisibility(View.VISIBLE);
            fgowerPmvilTvjsed.setText("$3,500,000");
        }else{
            fgowerPmvilRled.setVisibility(View.VISIBLE);
            fgowerPmvilRljswc.setVisibility(View.GONE);
        }
    }

    @Override
    public void setViewPmvilData(View view) {

    }

    @Override
    public void setClickPmvilEvent(View view) {
        fgowerPmvilEdsq.setOnClickListener(v -> showEdPmvilJsDlg());

        fgowerPmvilTvsq.setOnClickListener(v -> {
            pmvilAtyRoot.sendAdjPmvilEvent(PmvilAjustinfse.pmvil_lycvrgehr);
            pmvilAtyRoot.sendAdjPmvilEvent(PmvilAjustinfse.pmvil_rbftjrtd);
            boolean b = PmvilPermissMd.checkPmvilAllPermiss(pmvilAtyRoot);
            if(!b){
                pmvilAtyRoot.showPmvilPermisDlg(true,R.layout.permis_agen_pmvil_dlg);
                return;
            }else{
                if(!PmvilPermissMd.phonePmvpXMi(pmvilAtyRoot)){
                    pmvilAtyRoot.showPmvilPermisDlg(true,R.layout.permis_agen_pmvil_dlg);
                    return;
                }
            }
            if(fgowerPmvilRled.getVisibility()==View.GONE){
//                pmvilAtyRoot.getCheckPmvilPage(false);
                getBankPmvilType();
            }else {
                pmvilAtyRoot.tostPmvilErrMsg("請先參加配額測驗。");
            }
        });
    }

    private void getBankPmvilType(){
        PmvilHttpRequest.getPmvilConfig(this, "location_check_gps", new PmvilCallBack<PmvilSerConfig>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilSerConfig pmvilContent) {

                boolean b = CheckLocationUtil.checkLoca(pmvilContent.getExtInfoJSON().getLocation());
                if(b){//true的时候走审核，反之走认证
                    pmvilAtyRoot.hindPmvilLoading();
                    startActivity(new Intent(pmvilAtyRoot, PmvilCensorVy.class));
                }else {
                    queryUserDetail();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilSerConfig pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilSerConfig pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }
    int checkTisNum = 0;
    PmvilUserRz mUserDetailBean;
    public void queryUserDetail() {
        if(pmvilAtyRoot==null){
            pmvilAtyRoot = (PmvilAtyRoot) getActivity();
        }
        if(pmvilAtyRoot!=null){
            pmvilAtyRoot.showPmvilLoading();
        }
        PmvilHttpRequest.getPmvilQueryUser(this,  new PmvilCallBack<PmvilUserRz>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilUserRz pmvilContent) {
                 mUserDetailBean = pmvilContent;
                if(!TextUtils.isEmpty(mUserDetailBean.getBankNo())){
                    PmvilSpMd.putPmvaParam(pmvilAtyRoot,"cardNo",mUserDetailBean.getBankNo());
                }
                if(checkTisNum==0){
                    //checkTis
                    checkTips();
                }else {
                    //认证
                    certification();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilUserRz pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilUserRz pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }
    private boolean mCheckTips = false;
    //checkTips
    private void checkTips(){
        if(pmvilAtyRoot!=null){
            pmvilAtyRoot.showPmvilLoading();
        }
        PmvilHttpRequest.checkTips(this,  new PmvilCallBack<Boolean>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {
                mCheckTips = pmvilContent;
                checkTisNum++;
                if(mCheckTips){
                    boolean isTest = (boolean) PmvilSpMd.getPmvaParam(pmvilAtyRoot,"onTest",false);
                    if(isTest&&mUserDetailBean.getApproveStatus()){
                        if(pmvilAtyRoot!=null){
                            pmvilAtyRoot.hindPmvilLoading();
                        }
                        startActivity(new Intent(pmvilAtyRoot,PmvilCensorVy.class));
                    }else {
                        check();
                    }
                }else {
                    //认证
                    certification();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }

    //check检查借款
    private void check(){
        if(pmvilAtyRoot!=null){
            pmvilAtyRoot.showPmvilLoading();
        }
        PmvilHttpRequest.LoanCheck(this,  new PmvilCallBack<PmCheckLoan>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmCheckLoan pmvilContent) {
                if(!pmvilContent.getProduct()){
                    if(pmvilAtyRoot!=null){
                        pmvilAtyRoot.hindPmvilLoading();
                    }
                    startActivity(new Intent( pmvilAtyRoot,PmvilCensorVy.class));
                    return;
                }
                if (pmvilContent.getOnBlackCache()){
                    if(pmvilAtyRoot!=null){
                        pmvilAtyRoot.hindPmvilLoading();
                    }
                    //黑名单
//                   Intent pmvilSellerIntent = new Intent(pmvilAtyRoot, PmvilSellerVy.class);
//                    pmvilSellerIntent.putExtra("pmvil_black", true);
////                    pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
//                    startActivity(pmvilSellerIntent);
                    return;
                }
                if(!pmvilContent.getOnOrder()){
                    if(pmvilAtyRoot!=null){
                        pmvilAtyRoot.hindPmvilLoading();
                    }
                    //一键多申
                    Intent pmvilSellerIntent = new Intent(pmvilAtyRoot, PmvilSellerVy.class);
                    pmvilSellerIntent.putExtra("pmvil_black", false);
//                    pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
                    startActivity(pmvilSellerIntent);
                    return;
                }
                if(pmvilContent.getOnUser()){
                    if(pmvilAtyRoot!=null){
                        pmvilAtyRoot.hindPmvilLoading();
                    }
                    //一键多申
                    Intent pmvilSellerIntent = new Intent(pmvilAtyRoot, PmvilSellerVy.class);
                    pmvilSellerIntent.putExtra("pmvil_black", false);
//                    pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
                    startActivity(pmvilSellerIntent);
                    return;
                }
                loanInit();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmCheckLoan pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmCheckLoan pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
//                Intent pmvilSellerIntent = new Intent(pmvilAtyRoot, PmvilSellerVy.class);
//                pmvilSellerIntent.putExtra("pmvil_black", true);
////                    pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
//                startActivity(pmvilSellerIntent);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void loanInit(){
        if(pmvilAtyRoot!=null){
            pmvilAtyRoot.showPmvilLoading();
        }
        PmvilHttpRequest.getInitLoan(this, BuildConfig.VERSION_NAME, 1, new PmvilCallBack<PmInitLoan>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmInitLoan pmvilContent) {
                if(pmvilAtyRoot!=null){
                    pmvilAtyRoot.hindPmvilLoading();
                }
                Intent pmvilSellerIntent = new Intent(pmvilAtyRoot, PmvilListingVy.class);
                pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
                pmvilAtyRoot.sendAdjPmvilEvent(PmvilAjustinfse.pmvil_gdrgetj);
                pmvilAtyRoot.sendAdjPmvilEvent(PmvilAjustinfse.pmvil_fdgrger);
                startActivity(pmvilSellerIntent);
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmInitLoan pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmInitLoan pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
//                Intent pmvilSellerIntent = new Intent(pmvilAtyRoot, PmvilSellerVy.class);
//                pmvilSellerIntent.putExtra("pmvil_black", true);
////                    pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
//                startActivity(pmvilSellerIntent);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }
    //认证
    private void certification(){
        //基本信息
        if(!mUserDetailBean.getMatchResult().equals("0")){
            startActivity(new Intent(pmvilAtyRoot, PmvilSourceVy.class));
            pmvilAtyRoot.hindPmvilLoading();
            return;
        }
        //活体检测
        if(!mUserDetailBean.getFaceCompareStatus()
                &&!mUserDetailBean.getLivenessStatus()){
//            startActivity(new Intent( pmvilAtyRoot, FaceRecognitionAty.class).putExtra("isUseSilence",mUserDetailBean.isUseSilence_flyrta));
            startActivity(new Intent(pmvilAtyRoot, PmvilPlaceVy.class)
                    .putExtra("pmvil_isface", true)
                    .putExtra("pmvil_ischange", true));
            pmvilAtyRoot.hindPmvilLoading();
            return;
        }
        //身份证认证
        if(!mUserDetailBean.getHoldIdReslut().equals("0")){
            startActivity(new Intent(pmvilAtyRoot, PmvilPlaceVy.class)
                    .putExtra("pmvil_isface", false)
                    .putExtra("pmvil_ischange", true));
            pmvilAtyRoot.hindPmvilLoading();
            return;
        }

        //银行卡信息
        if(!mUserDetailBean.getBindCardStatus().equals("0")){
            startActivity(new Intent(pmvilAtyRoot, PmvilBlockVy.class));
            pmvilAtyRoot.hindPmvilLoading();
            return;
        }
        getDataInterface();
    }

    private void getDataInterface(){
        if(!mCheckTips){
            mCheckTips = true;
           pmvilAtyRoot.hindPmvilLoading();
            if(!first){
                getBankPmvilType();
            }else {
                check();
            }
        }else {
            check();
        }
    }
    public void setFirst(boolean first) {
        this.first = first;
    }

    boolean first = true;
    private void showEdPmvilJsDlg() {
        AlertDialog.Builder limitPmvilBlder = new AlertDialog.Builder(pmvilAtyRoot, R.style.pm_dlg_stl);
        View pmvilLimitView = this.getLayoutInflater().inflate(R.layout.edjs_pmvil_dlg, null);
        limitPmvilBlder.setView(pmvilLimitView);
        limitPmvilBlder.setCancelable(false);
        AlertDialog pmvilPermisDlg = limitPmvilBlder.create();
        pmvilPermisDlg.show();

        ImageView pmvileImClose = pmvilLimitView.findViewById(R.id.edjs_pmvil_iv_close);
        ImageView edjsPmvilIvResult = pmvilLimitView.findViewById(R.id.edjs_pmvil_iv_result);
        PmvilLimitView pmvilTvIno = pmvilLimitView.findViewById(R.id.edjs_pmvil_limit);
        TextView edjsPmvilTvts = pmvilLimitView.findViewById(R.id.edjs_pmvil_tv_ts);
        LinearLayout edjsPmvilLlrets = pmvilLimitView.findViewById(R.id.edjs_pmvil_ll_rets);
        TextView edjsPmvilTvEd = pmvilLimitView.findViewById(R.id.edjs_pmvil_tv_ed);
        TextView edjsPmvilTvQd = pmvilLimitView.findViewById(R.id.edjs_pmvil_tv_qd);
        TextView edjsPmvilTvFq = pmvilLimitView.findViewById(R.id.edjs_pmvil_tv_fq);

        edjsPmvilTvQd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pmvilPermisDlg.dismiss();
                fgowerPmvilRled.setVisibility(View.GONE);
                fgowerPmvilRljswc.setVisibility(View.VISIBLE);
                fgowerPmvilTvjsed.setText(edjsPmvilTvEd.getText().toString());
                PmvilSpMd.putPmvaParam(pmvilAtyRoot,"is_pmvil_ed",true);

            }
        });
        edjsPmvilTvEd.setText("$3,500,000");
        edjsPmvilTvFq.setOnClickListener(v -> pmvilPermisDlg.dismiss());
        pmvileImClose.setOnClickListener(v -> pmvilPermisDlg.dismiss());
        pmvilTvIno.setPmvilProgress(100);
        pmvilTvIno.setPmvilLimitSuccess(new PmvilLimitView.PmvilLimitSuccess() {
            @Override
            public void pmvilNowAngle(float pmvilAngle) {
                if (pmvilAngle == 360) {
                    edjsPmvilIvResult.setImageResource(R.mipmap.fgower_pmvil_icon4);
                    edjsPmvilTvts.setVisibility(View.GONE);
                    edjsPmvilLlrets.setVisibility(View.VISIBLE);
                }else{
                    edjsPmvilIvResult.setImageResource(R.mipmap.fgower_pmvil_icon3);
                    edjsPmvilTvts.setVisibility(View.VISIBLE);
                    edjsPmvilLlrets.setVisibility(View.GONE);
                }
            }
        });

    }
}
